package com.aliyun.config;

import com.aliyun.AliyunTool;
import com.aliyun.core.AliyunContext;
import com.aliyun.core.impl.AliyunOssContext;
import com.aliyun.core.impl.AliyunSmsContext;
import com.aliyun.handler.AliyunContextHolder;
import com.aliyun.properties.AliyunProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class AliyunConfiguration {

    @Bean
    public AliyunTool aliyunTool(){
        return new AliyunTool();
    }

    @Bean
    public AliyunProperties aliyunProperties(){
        return new AliyunProperties();
    }

    @Bean
    public AliyunContextHolder aliyunContextHolder(){
        return new AliyunContextHolder();
    }

    @Bean(name = "aliyun-context-oss")
    public AliyunContext aliyunOssContext(){
        return new AliyunOssContext();
    }

    @Bean(name = "aliyun-context-sms")
    public AliyunContext aliyunSmsContext(){
        return new AliyunSmsContext();
    }

    @Bean
    @ConditionalOnMissingBean(value = ThreadPoolExecutor.class)
    public ThreadPoolExecutor threadPoolExecutor() {
        int processors = Runtime.getRuntime().availableProcessors();
        return new ThreadPoolExecutor(processors,
                processors * 2,
                5,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(processors * 2),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy());
    }
}
